/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.tools;

import cn.ac.iie.di.tools.FileTools;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author lenovo-pc
 */
public class JsonSplitor implements Runnable {

    String dir = null;
    String desdir = null;
    ArrayBlockingQueue msgQueue = null;
    String FileString = null;

    static Logger logger = null;

    static {
        logger = org.apache.log4j.Logger.getLogger(JsonSplitor.class.getName());
    }

    public JsonSplitor(String dir, String desdir, String filetype, ArrayBlockingQueue msgQueue) {
        this.dir = dir;
        this.msgQueue = msgQueue;
        this.desdir = desdir;
        this.FileString = filetype;
    }

    public void run() {
        while (true) {
//            logger.info("the json parser dir is " + dir);
            List<File> sigFiles = FileTools.getFiles(new File(dir), "log");

            for (File sigFile : sigFiles) {
                File filesig = new File(desdir + File.separator + sigFile.getName() + ".sig");

                if (!filesig.exists()) {
                    try {
                        logger.info("file:" + sigFile.getPath());
                        while (!msgQueue.offer(sigFile.getName())) {
                        }
                        while (!FileTools.creatTxtFile(filesig.getPath())) {
                            logger.info(".sig Create failed :" + filesig.getPath());
                            Thread.sleep(500);
                        }
                        if (!filesig.exists()) {
                            logger.info("xxxxxxxx " + filesig.getPath() + " no file");
                        }

                    } catch (IOException ex) {
                        logger.error("JsonSplitor Exception " + ex, ex);
                    } catch (InterruptedException ex) {
                        java.util.logging.Logger.getLogger(JsonSplitor.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

            }
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ex) {
            }
        }
    }

    public void readFileByLines(String fileName, String newFileName, String FileType) {

        BufferedReader reader = null;
        String filetype = null;
        filetype = FileType;
        try {
            long begin = System.currentTimeMillis();
            //System.out.println("以行为单位读取文件内容，一次读一整行：");
            //reader = new BufferedReader(new FileReader(fileName));
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
            String tempString = null;
            int line = 0;
            // 一次读入一行，直到读入null为文件结束  
            if (fileName.contains("message")) {
                while ((tempString = reader.readLine()) != null) {
                    try {
                        while (!msgQueue.offer(tempString)) {
                            Thread.sleep(1000);
                            logger.warn(fileName + "Put Json Array Block buffer Fail!");
                        }
                    } catch (InterruptedException ex) {
                        logger.error("the error is :" + ex, ex);
                    }
                    line++;
                }
            }
//            while ((tempString = reader.readLine()) != null) {
//                try {
//                    if (fileName.contains("message")) {
//                        while (!msgQueue.offer(tempString)) {
//                            Thread.sleep(1000);
//                            logger.warn("Put Json Array Block buffer Fail!");
//                        }
//                    }
//                    if (fileName.contains("userprofile")) {
//                        while (!userQueue.offer(tempString)) {
//                            Thread.sleep(1000);
//                            logger.warn("Put Json Array Block buffer Fail!");
//                        }
//                    }
//
//                } catch (InterruptedException ex) {
//                }
//                line++;
//            }
            reader.close();
            logger.info("There is " + line + " in " + fileName + "！ ");
            //将文件移动走

            FileTools.moveFileToNewPath(fileName, newFileName);
            String okfileName = fileName.split("json")[0] + "ok";
            FileTools.moveFileToNewPath(okfileName, newFileName);
            long end = System.currentTimeMillis();
            System.out.println("consume " + (end - begin) + " ms");
        } catch (Exception e) {
            logger.error("the error is :" + e, e);
        }
    }
}
